package service

import (
	"context"
	"encoding/json"
)

type Module interface {
	Start(ctx context.Context, config *Config, s *Service) error
	Close() error
	IsRunning() bool
	Name() string
}

type MysqlConfig struct {
	User  string `json:"userName"`
	Pass  string `json:"userPassword"`
	IP    string `json:"host,omitempty"`
	Port  int    `json:"port,omitempty"`
	Table string `json:"tableName,omitempty"`
}

type RedisConfig struct {
	IP   string `json:"ip,omitempty"`
	Port int    `json:"port,omitempty"`
}

type HttpConfig struct {
	IP   string `json:"ip,omitempty"`
	Port string `json:"port,omitempty"`
	Host string `json:"host"`
	SSH  bool   `json:"ssh,omitempty"`
	CRT  string `json:"crt,omitempty"`
	KEY  string `json:"key,omitempty"`
}

type PlatonConfig struct {
	KafkaIP    string `json:"kafka_ip,omitempty"`
	KafkaPort  string `json:"kafka_port,omitempty"`
	KafkaTopic string `json:"kafka_topic,omitempty"`
}

type ModuleType int

func (mt ModuleType) String(mtype int) string {
	var typeString = ""
	switch mtype {
	case MYSQLTYPE:
		typeString = "Mysql"
	case REDISTYPE:
		typeString = "redis"
	case HTTPTYPE:
		typeString = "http"
	case CENTERRPC:
		typeString = "centerrpc"
	default:
		typeString = "unknown"
	}

	return typeString
}

const (
	MYSQLTYPE = iota
	REDISTYPE
	HTTPTYPE
	PLATONTYPE
	CENTERRPC
)

type Config struct {
	Mysql  MysqlConfig
	Redis  RedisConfig
	Http   HttpConfig
	Platon PlatonConfig

	RpcURL string

	Modules []ModuleType `json:"modules,omitempty"`
}

func (c *Config) ConfigToJSON() ([]byte, error) {
	return json.Marshal(*c)
}

func (c *Config) ConfigFromJSON(data []byte) error {
	return json.Unmarshal(data, c)
}
